<script src="<?php echo base_url().'assets/js/jquery.tablesorter.min.js'; ?>" type="text/javascript"></script>
<script type="text/javascript">
	$(function(){
		$('#delete').click(function(){
			var n
			n = $("input[name='checkid[]']:checked").length
			if( n <= 0 ) {
				alert('Please select users from the list to delete.')
				return false
			} else {
				if ( confirm('Are you sure you want to delete records?') == true ){
					$('#task').val('delete')
					$('#adminForm').submit()
				} else {
					return false
				}
			}
			return false
		})

		$('#checkall').click(function(){
			var n
			n = $("input[name='checkid[]']").length
			for( var i=0; i<n; i++){
				if( $("#checkall").is(":checked") ){
					$("input[name='checkid[]']").attr('checked', true);
				} else {
					$("input[name='checkid[]']").attr('checked', false);
				}
			}
			$("#checkboxall").removeClass("checkall").addClass("uncheckall");
		})

		$("#adminTable").tablesorter({
			 headers: {
               0: {sorter: false},
               1: {sorter: false},
               2: {sorter: false},
               6: {sorter: false},
             }
		})

		$('#search-btn').click( function(){
			$('#adminForm').submit()
			return false;
		})

		$('#reset-btn').click( function(){
			$('#search-text').val('')
			$('#adminForm').submit()
			return false;
		})

	})

	function toggle(task, postid){
		var url = '<?php echo base_url('admin/posts/toggle'); ?>'
		$.ajax({
			url: url,
			type:'post',
			data: 'task='+task+'&postid='+postid,
			success:function(data){
		//alert(data);return false;
				$("#toggle_"+postid).html(data)
			}
		})
	}


</script>
<div class="content">
	<?php echo form_open('admin/posts/index', array('name'=>'adminForm','id'=>'adminForm')); ?>
	<div id="top-heading">
		<div id="h-links">
			<?php
			echo anchor('', 'Delete', 'id="delete" class="button"');
			echo form_input('search_text',$this->input->post('search_text'),'id="search-text"');
			echo '<a href="javascript:void(0);" id="search-btn" class="button">Search<a/>';
			echo '<a href="javascript:void(0);" id="reset-btn" class="button">Reset<a/>';
			?>
		</div>
		<div id="pagination">
		<?php echo $pagination; ?>
		</div>
	</div>

	<?php
	$this->message->display();
	$this->load->library('table');
	$tmpl = array( 'table_open' => '<table border="1" class="admin-table" id="adminTable">' );
	$this->table->set_template($tmpl);
	$h = array();
	$h[] = array('data' => '#', 'width' => '3%');
	$h[] = array('data' => '<input type="checkbox" id="checkall" class="inputbox"/>', 'width' => '3%');
	$h[] = array('data' => 'View', 'width' => '5%');
	$h[] = array('data' => 'Posted by');
	$h[] = array('data' => 'Content');
	$h[] = array('data' => 'Posted to');
	$h[] = array('data' => 'Publish', 'width' => '5%');
	$h[] = array('data' => 'ID', 'width' => '5%');

	$this->table->set_heading($h);

	$table_row = array();
	$i = 1;
	if(!empty($posts_qry_limited->num_rows)){echo'hoi';
		foreach ($posts_qry_limited->result() as $row){
			$table_row = null;
			$table_row[] = $i++;
			$table_row[] = '<input type="checkbox" name="checkid[]" value="'.$row->postid.'" class="checkbox" />';
			$table_row[] = anchor('admin/posts/show/'.$row->postid, 'View');
			$table_row[] = '<span>'.$row->userfname.' '.$row->userlname.'</span><br><span>Date: '.date('m/d/Y',strtotime($row->date_added)).'</span>';
			$table_row[] = $row->message;
			$table_row[] = '<span>'.$row->post_to.'</span><br><span class="add-details">'.date('M d, Y',strtotime($row->birthday)).' - '.date('M d, Y',strtotime($row->date_of_death)).'</span>';
			$this->load->helper('Boolean_helper');
			$table_row[] = Boolean_helper::status($row->status, true, $row->postid);
			$table_row[] = $row->postid;
			$this->table->add_row($table_row);
		}
	} else {
		$table_row = null;
		$table_row[] = array('data' => 'No record found.', 'colspan' => '8');
		$this->table->add_row($table_row);
	}
	echo $this->table->generate();
	?>
	<input type="hidden" name="task" id="task" />
	<?php echo form_close();
	?>

</div>
